package com.krt.webapp.until;

import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


/**
 * �Գ���Կ�ļ��ܺͽ���
 * 
 * @author yaodaqing
 * 
 */
public class AES {

	
    /**��������ת����16����
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < buf.length; i++) {
                    String hex = Integer.toHexString(buf[i] & 0xFF);
                    if (hex.length() == 1) {
                            hex = '0' + hex;
                    }
                    sb.append(hex.toUpperCase());
            }
            return sb.toString();
    }
    
    /**��16����ת��Ϊ������
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
            if (hexStr.length() < 1)
                    return null;
            byte[] result = new byte[hexStr.length()/2];
            for (int i = 0;i< hexStr.length()/2; i++) {
                    int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
                    int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
                    result[i] = (byte) (high * 16 + low);
            }
            return result;
    }
    
	/**
	 * ����
	 * 
	 * @param content
	 * @param aes
	 * @return
	 */
	public static byte[] getAESEncrypt(byte[] content, byte[] aes) {
		if (content == null || content.length < 0 || aes == null || aes.length < 0) {
			System.out.println("��Կ������Ϊ��...");
			return null;
		}
		try {
			//����16λ
			byte[] content_temp;
			int w=content.length%16;
			if(w!=0){
				content_temp=new byte[content.length+16-w];
				for(int i=0;i<content.length;i++){
					content_temp[i]=content[i];
				};
			}
			else
				content_temp=content;
				
			SecretKeySpec sks = new SecretKeySpec(aes, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
			cipher.init(Cipher.ENCRYPT_MODE, sks);
			byte[] jiamihou = cipher.doFinal(content_temp);
			return jiamihou;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * ����
	 * 
	 * @param content
	 * @param aes
	 * @return
	 */
	public static byte[] getAESDecrypt(byte[] content, byte[] aes) {
		if (content == null || content.length == 0 || aes == null || aes.length < 0) {
			System.out.println("��Կ������Ϊ��...");
			return null;
		}
		try {
			SecretKeySpec sks = new SecretKeySpec(aes, "AES");	
			Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");	//AES/ECB/NoPadding
			cipher.init(Cipher.DECRYPT_MODE, sks);
			byte[] jiemihou = cipher.doFinal(content);
			//ȥ�������ֽ�(0)
			
			
			return jiemihou;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * ����
	 * 
	 * @param content
	 * @param aes
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String getAESEncrypt(String content,String aes) throws UnsupportedEncodingException{
		return parseByte2HexStr(getAESEncrypt(content.getBytes("gbk"),aes.getBytes()));
		
	}
	
	/**
	 * ����
	 * 
	 * @param content
	 * @param aes
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String getAESDecrypt(String content,String aes) throws UnsupportedEncodingException{
		return new String(getAESDecrypt(parseHexStr2Byte(content),aes.getBytes()),"gbk").replaceAll("\0", "");
	}

    
	public static void main(String[] args) throws UnsupportedEncodingException {
		String aes="My Password22222";	//����Ϊ16λ
		
		System.out.println("���ܺ���ݣ�"+getAESEncrypt("mybook tank=mybook tank=",aes));
		
		System.out.println("���ܺ���ݣ�"+getAESDecrypt("329C845457CD7A828BA1D8F612DC7C2FB4606A7D99F90A0B1562623E8FD6CD02",aes));

		
	}
}